import { _decorator, CCInteger, Component, EventKeyboard, Input, input, KeyCode, Node, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('move')
export class move extends Component {

    @property(CCInteger)
    speed: number = 0;

    private pos: Vec3 = new Vec3();
    start() {
        input.on(Input.EventType.KEY_DOWN, this.getTo, this);
        this.pos = this.node.position;
    }

    getTo(event: EventKeyboard){
        switch(event.keyCode){
            case KeyCode.KEY_W:
                this.pos = new Vec3(this.pos.x, this.pos.y+this.speed, 0);
                break;
            case KeyCode.KEY_A:
                this.pos = new Vec3(this.pos.x-this.speed, this.pos.y, 0);
                break;
            case KeyCode.KEY_S:
                this.pos = new Vec3(this.pos.x, this.pos.y-this.speed, 0);
                break;
            case KeyCode.KEY_D:
                this.pos = new Vec3(this.pos.x+this.speed, this.pos.y, 0);
                break;
        }
        this.node.setPosition(this.pos);
    }

    update(deltaTime: number) {
        
    }
}


